package com.bysj.sausys.controller.backend;

import com.bysj.sausys.beans.PageQuery;
import com.bysj.sausys.beans.PageResult;
import com.bysj.sausys.common.JsonData;
import com.bysj.sausys.common.UserConst;
import com.bysj.sausys.model.SysUser;
import com.bysj.sausys.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 系统用户controller
 *
 * @author WangZK
 */
@Api(description = "系统用户接口")
@Controller
@RequestMapping("/sys/user")
public class SysUserController {

    @Resource
    private ISysUserService sysUserService;

    /**
     * 系统用户保存
     *
     * @param sysUser
     * @param request
     * @return
     */
    @ApiOperation(value = "保存系统用户接口（超级管理员）")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public JsonData saveUser(@ApiParam(value = "系统用户实体", required = true) @RequestBody SysUser sysUser,
                             HttpServletRequest request) {
        // 从session中获取当前用户
        Map map = UserConst.getCurrentUser(request);
        sysUserService.save(sysUser, UserConst.getCurrentUsername(map));
        return JsonData.success();
    }

    /**
     * 系统用户更新
     *
     * @param sysUser
     * @param request
     * @return
     */
    @ApiOperation(value = "更新系统用户接口（超级管理员）")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @ResponseBody
    public JsonData updateUser(@ApiParam(value = "系统用户实体", required = true) @RequestBody SysUser sysUser,
                               HttpServletRequest request) {
        // 从session中获取当前用户
        Map map = UserConst.getCurrentUser(request);
        sysUserService.update(sysUser, UserConst.getCurrentUsername(map));
        return JsonData.success();
    }

    /**
     * 获取系统用户列表
     *
     * @param categoryId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "获取系统用户列表接口（超级管理员）")
    @RequestMapping(value = "/getPage", method = RequestMethod.GET)
    @ResponseBody
    public JsonData getPage(@ApiParam(value = "用户类型id") @RequestParam(required = false) String categoryId,
                            @ApiParam(value = "页码") @RequestParam(required = false, defaultValue = "1") int pageNo,
                            @ApiParam(value = "每页显示条数") @RequestParam(required = false, defaultValue = "10") int pageSize) {
        PageResult result = sysUserService.getPageByCategoryId(categoryId, new PageQuery(pageNo, pageSize));
        return JsonData.success(result);
    }

    /**
     * 启用系统用户
     *
     * @param id
     * @param request
     * @return
     */
    @ApiOperation(value = "启用系统用户接口（超级管理员）")
    @RequestMapping(value = "/enable", method = RequestMethod.PUT)
    @ResponseBody
    public JsonData enable(@ApiParam(value = "用户id", required = true) @RequestParam Integer id,
                           HttpServletRequest request) {
        // 从session中获取当前用户
        Map map = UserConst.getCurrentUser(request);
        sysUserService.updateStatusByUserId(id, UserConst.ENABLE, UserConst.getCurrentUsername(map));
        return JsonData.success();
    }

    /**
     * 禁用系统用户
     *
     * @param id
     * @param request
     * @return
     */
    @ApiOperation(value = "禁用系统用户接口（超级管理员）")
    @RequestMapping(value = "/disable", method = RequestMethod.PUT)
    @ResponseBody
    public JsonData disable(@ApiParam(value = "用户id", required = true) @RequestParam Integer id,
                            HttpServletRequest request) {
        // 从session中获取当前用户
        Map map = UserConst.getCurrentUser(request);
        sysUserService.updateStatusByUserId(id, UserConst.DISABLE, UserConst.getCurrentUsername(map));
        return JsonData.success();
    }

    /**
     * 根据id获取用户信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id获取用户信息接口（超级管理员）")
    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    @ResponseBody
    public JsonData getOne(@ApiParam(value = "用户id", required = true) @RequestParam Integer id) {
        SysUser sysUser = sysUserService.findById(id);
        return JsonData.success(sysUser);
    }

    /**
     * 重置密码
     *
     * @param id
     * @param request
     * @return
     */
    @ApiOperation(value = "重置密码接口（超级管理员）")
    @RequestMapping(value = "/resetPsw", method = RequestMethod.PUT)
    @ResponseBody
    public JsonData resetPsw(@ApiParam(value = "用户id", required = true) @RequestParam Integer id,
                             HttpServletRequest request) {
        // 从session中获取当前用户
        Map map = UserConst.getCurrentUser(request);
        sysUserService.resetPsw(id, UserConst.getCurrentUsername(map));
        return JsonData.success();
    }

    /**
     * 修改密码
     *
     * @param newPwd
     * @param request
     * @return
     */
    @ApiOperation(value = "修改密码接口（已登录的用户）")
    @RequestMapping(value = "/updatePsw", method = RequestMethod.PUT)
    @ResponseBody
    public JsonData updatePsw(@ApiParam(value = "新密码", required = true) @RequestParam String newPwd,
                              HttpServletRequest request) {
        // 从session中获取当前用户
        Map map = UserConst.getCurrentUser(request);
        Integer id = (Integer) map.get("id");
        sysUserService.updatePsw(id, newPwd, UserConst.getCurrentUsername(map));
        request.getSession().invalidate();
        return JsonData.success();
    }

}
